<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">

<title><%= htmlTitle %></title>

<link rel="stylesheet" href="/assets/stylesheets/style.css">
<script src="/assets/lib/jquery/3.6.0/jquery.min.js"></script>
<script src="/assets/lib/axios/0.26.1/axios.min.js"></script>

<!-- refer: https://www.sweetalert.cn/ -->
<script src="/assets/lib/sweetalert/2.1.2/sweetalert.min.js"></script>

<script src="/assets/javascripts/httpRequestUtils.js"></script>
<script src="/assets/javascripts/localStorageUtils.js"></script>
<script>
    // API地址
    const APIHOST = '<%= global.site.api.prefix %>';
    axios.defaults.baseURL = APIHOST;
</script>
<% if (group == "admin") {%>
<script>
    if(localStorageUtils.getIsUser()) {
        // 是普通用户，跳转到普通用户后台页面
        window.location.href = "/dashboard/user/index";
    }
</script>
<%} else {%>
<script>
    if(localStorageUtils.getIsAdmin()) {
        // 是管理员用户，跳转到管理员用户后台页面
        window.location.href = "/dashboard/admin/index";
    }
</script>
<%}%>
<script>
    function getUserStatus() {
        localStorageUtils.checkLocalStorage();

        if(!localStorageUtils.getLoginStatus()) {
            localStorageUtils.userLogout();
            window.location.href = "/";
        }

        postRequest("/user/getUserStatus", { token: localStorageUtils.getToken() })
            .then(function (responseData) {
                var axiosData = responseData.data;
                var status = axiosData.status;
                var data = axiosData.data;
                if (status === "success") {
                    console.log(data)
                    if (data) {
                        document.getElementById("nickname").innerHTML = "当前登录用户：" + data.nickname;
                    } else {
                        window.location.href = "/login";
                    }
                } else {
                    swal(`出错啦！${data.errMsg} (错误码: ${data.errCode})`).then(function () {
                        if(data.errCode == "20004") { // 登录过期
                            localStorageUtils.userLogout();
                            window.location.href = "/login?redirect=" + encodeURIComponent(location.pathname + location.search);
                        }
                    });
                }
            }).catch(function (error) {
                console.log(error);
                swal("无法连接到服务器，请检查网络连接！");
            });
    }
    getUserStatus();
</script>